<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
时间：<span id="time"></span>
<script src="common.js"></script>
<script>

    //            var time = my$("time");
    //            setInterval(function () {
    //                //每过一秒钟，给span设置一次时间
    //                var date = new Date();
    //                setInnerText(time, getTimeString(date));
    //            },1000);

    //        function setInner(element,content) {
    //            if (typeof element.textContent === "string") {
    //                element.textContent = content;
    //            }else{
    //                element.innerText = content;
    //            }
    //        }

    //    var time = my$("time");
    //    time.innerHTML = getTimeString(new Date())
    //    setInterval(function () {
    ////        var date = new Date();
    ////        setInnerText(time, getTimeString(date))
    //        time.innerHTML = getTimeString(new Date())
    //    }, 1000);

    var time = document.getElementById("time");
    setInnerText(time, getTimeString(new Date()))
    setInterval(function () {
        var date = new Date()
        setInnerText(time, getTimeString(date))
    }, 1000);


    /**
     * 把日期对象转换成日期字符串
     * @param date
     * @returns {string}
     */
    function getTimeString(date) {
        //年
        var year = date.getFullYear();
        //月
        var month = date.getMonth() + 1; //月份从0开始
        //日
        var day = date.getDate();
        //小时
        var hour = date.getHours();
        //分钟
        var minute = date.getMinutes();
        //秒
        var second = date.getSeconds();
        //如果不够两位补0
        month = month < 10 ? "0" + month : month;
        day = day < 10 ? "0" + day : day;
        hour = hour < 10? "0" + hour : hour;
        minute = minute < 10 ? "0" + minute : minute;
        second = second < 10 ? "0" + second : second;
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }

    //设置innerText
    /**
     * @param element
     * @param content
     */
    function setInnerText(element,content) {
        if (typeof element.textContent === "string") {
            element.textContent = content;
        }else{
            element.innerText = content;
        }
    }

</script>
</body>
</html>